package com.mach.platform.controller;

import com.mach.platform.domain.AlarmMessage;
import com.mach.platform.domain.Response;
import com.mach.platform.service.AlarmMessageServiceImpl;
import com.mach.platform.service.AlarmServiceForeignImpl;
import com.mach.platform.service.AlarmServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;

/**
 * 报警信息控制器
 */

@RestController
@RequestMapping("/alarmMessage")
public class AlarmMessageCtrl extends BaseCtrl<AlarmMessage>{

    @Autowired
    private AlarmMessageServiceImpl alarmMessageServiice;

    @Autowired
    private AlarmServiceForeignImpl alarmForeignService;

    @Override
    public Response info(Long id) {
        return null;
    }

    @ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
    @GetMapping(value = "/list")
    @Override
    public Response list(@PageableDefault(size = 14, sort = {"alarmMessageTime"}, direction = Sort.Direction.DESC) Pageable pageable, @RequestParam Map params) {
        Page<AlarmMessage> result = alarmMessageServiice.list(pageable, params);
        return Response.ok(result);
    }

    @ApiOperation(value = "下载表格", produces = "application/json")
    @PostMapping(value = "/downloadTable")
    public void downloadTable(@SortDefault(sort = {"alarmMessageTime"}, direction = Sort.Direction.DESC) Sort sort
            , @RequestParam Map params, HttpServletResponse response){
        alarmMessageServiice.downloadTable(sort, params, response);
    }


    /**
     * 复位报警源，设置全部报警设备的属性一小时内不报警
     * @return
     */
    @ApiOperation(value = "复位报警源，设置设备的属性一小时内不报警")
    @PostMapping("/resetAlarmInfo")
    public Response resetAlarmInfo() {
        boolean flag = alarmForeignService.resetAlarmInfo();
        AlarmServiceForeignImpl.isAlarm = 1;
        AlarmServiceImpl.nowAlarmType = false;
        if (flag) {
            return Response.ok("复位报警源成功");
        }
        return Response.error("服务器异常");
    }


    @Override
    public Response delete(Long[] ids) {
        return null;
    }


    @Override
    public Response save(@RequestBody AlarmMessage alarmMessage) {
        return null;
    }

    @Override
    public Response update(AlarmMessage alarmMessage) {
        return null;
    }

    @ApiOperation(value = "处理结果和结果人", notes = "接口备注：无")
    @PostMapping("/update")
    public Response update(Long id, String dealPeople,String dealResult) {
        if (Objects.isNull(id)){
            return Response.error("id不能为空");
        }
        if (!StringUtils.isNoneBlank(dealPeople, dealResult)){
            return Response.error("处理内容和结果人不能为空");
        }
        String result = alarmMessageServiice.update(id, dealPeople, dealResult);
        if (StringUtils.equals(result, "处理成功")){
            return Response.ok(result);
        }
        return Response.error(result);
    }

}
